import { getC1, getC2, getC3 } from '@/utils/api/product/attr'
import { defineStore } from 'pinia'
import { CategoryObj } from '@/utils/api/product/attr/type'
import { AxiosResData } from '@/utils/api/common_type'

export type C1Type = {
  label: string
  value: string | number
}

// 分类
export const useCategoryStore = defineStore('category', {
  state: () => {
    return {
      c1: [] as C1Type[],
      c1Id: '',
      c2: [] as CategoryObj[],
      c2Id: '',
      c3: [] as CategoryObj[],
      c3Id: ''
    }
  },
  actions: {
    // 得到c1的选项
    async get_c1_async() {
      const res: AxiosResData<CategoryObj[]> = await getC1()
      if (res.data.code == 200) {
        ;(this.c1 as C1Type[]) = res.data.data.map((item: CategoryObj) => {
          return {
            label: item.name,
            value: item.id
          }
        })
      }
    },
    // 获取C2
    async get_c2_async() {
      const res: AxiosResData<CategoryObj[]> = await getC2(this.c1Id)
      if (res.data.code == 200) {
        this.c2 = res.data.data
      }
    },
    // 获取c3
    async get_c3_async() {
      const res: AxiosResData<CategoryObj[]> = await getC3(this.c2Id)
      if (res.data.code == 200) {
        ;(this.c3 as CategoryObj[]) = res.data.data
      }
    }
  }
})
